<template>
  <view style="width: 100%; height: 100%"><l-echart :ref="chartRef" @finished="initChart"></l-echart></view>
</template>

<script>
import * as echarts from '@/uni_modules/lime-echart/static/echarts.min';

export default {
  name: 'bar-chart',
  props: {
    chartRef: {
      type: String,
      default: 'chartBarRef'
    },
    chartData: {
      type: Object,
      default: () => {
        return {
          legendData: [],
          dataX: [],
          dataY: [],
          colorList: []
        };
      }
    },
    showBackground: {
      type: Boolean,
      default: false
    },
    barWidth: {
      type: Number,
      default: 17
    },
    barBorderRadius: {
      type: Number,
      default: 3
    }
  },
  watch: {
    chartData: {
      handler(val) {
        this.initChart();
      },
      deep: true
    }
  },
  data() {
    return {
      colorListDef: [
        {
          bar0: 'rgba(46, 125, 255, 1)',
          bar1: 'rgba(107, 58, 255, 1)'
        }
      ]
    };
  },
  mounted() {},
  methods: {
    async initChart() {
      if (!this.chartData.colorList || this.chartData.colorList.length == 0) {
        this.chartData.colorList = this.colorListDef;
      }
      let seriesData = this.chartData.dataY.map((item, inx) => {
        return {
          name: this.chartData.legendData[inx],
          type: 'bar',
          barWidth: this.barWidth,
          barGap: '20%', //柱间距离
          showBackground: this.showBackground,
          backgroundStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                { offset: 0, color: 'rgba(46, 125, 255, 0.1)' },
                { offset: 1, color: 'rgba(107, 58, 255, 0.1)' }
              ]
            },
            borderRadius: 3
          },
          label: {
            show: true,
            position: 'top',
            color: '#102C59'
          },

          itemStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                { offset: 0, color: this.chartData.colorList[inx].bar0 },
                { offset: 1, color: this.chartData.colorList[inx].bar1 }
              ]
            },
            barBorderRadius: this.barBorderRadius,
            borderWidth: 0
          },
          data: item
        };
      });
      let option = {
        tooltip: {
          trigger: 'axis'
        },
        grid: {
          left: '1%',
          right: '1%',
          bottom: '2%',
          top: this.chartData.legendData.length ? '18%' : '8%',
          containLabel: true
        },
        legend: {
          right: 0,
          itemGap: 16,
          itemWidth: 8,
          itemHeight: 8,
          icon: 'rect',
          data: this.chartData.legendData,
          textStyle: {
            fontFamily: 'Source Han Sans CN, Source Han Sans CN;',
            color: 'rgba(16, 44, 89, 0.5)',
            fontSize: 12
          }
        },
        xAxis: [
          {
            type: 'category',
            data: this.chartData.dataX,
            // boundaryGap: false, //两边留白
            axisLabel: {
              textStyle: {
                fontSize: 12,
                color: '#102C59'
              },
              interval: 0 //间隔
            },
            axisLine: {
              show: true,
              lineStyle: {
                type: 'dashed',
                color: 'rgba(0, 0, 0, 0.3)'
              }
            },
            axisTick: {
              show: false //小刻度
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            nameTextStyle: {
              color: 'rgba(16, 44, 89, 1)',
              align: 'right'
            },
            minInterval: 1, //自动计算的坐标轴最小间隔大小显示成整数
            axisLabel: {
              show: true,
              textStyle: {
                color: '#102C59',
                fontSize: 12
              },
              formatter: '{value}'
            },
            // splitNumber: 4,
            splitLine: {
              //网格线
              lineStyle: {
                show: true,
                type: 'dashed',
                opacity: 1,
                width: 1,
                color: 'rgba(0, 0, 0, 0.1)'
              }
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            }
          }
        ],

        dataZoom: this.chartData.dataX.length< 5 ? []:[
          {
            type: 'slider',
            show: false,
            startValue: this.chartData.dataX.length - 5,
            endValue: this.chartData.dataX - 1
          },
          {
            type: 'inside',
            startValue: this.chartData.dataX.length - 5,
            endValue: this.chartData.dataX - 1
          }
        ],
        series: seriesData
      };

      const chart = await this.$refs[this.chartRef].init(echarts);
      chart.setOption(option);
    }
  }
};
</script>

<style></style>
